#!/bin/bash
# ======================== package dir  ========================
TODAY=$(date +%Y%m%d)
startMS=`date +%s`
if [ ! -d "/opt/build/$TODAY" ];then
	mkdir /opt/build/$TODAY
	chmod -R 777 /opt/build/$TODAY
else
	chmod -R 777 /opt/build/$TODAY
fi
USER=${1}
PACKAGEVERSION=${2}
STRUCTUREVERSION=${3}
VERSIONUIC=${4}
VERSIONPLATFORM=${5}
VERSIONCAS=${6}
VERSIONSCORE=${7}
VERSIONTIMETABLE=${8}
VERSIONMICROSITE=${9}
VERSIONPORTAL=${10}
VERSIONOFFICE=${11}
VERSIONSTATIC=${12}
svnFolder=${13}
CURRENTTIME="$(date +%Y%m%d%H%M%S)";
echo $CURRENTTIME ----------- start download bxn-structure----------$svnFolder-$STRUCTUREVERSION
# ======================== fun getTime  ========================

export JAVA_HOME=/usr/java/jdk1.7.0_79
export M2_HOME=/usr/local/maven


BASE=/tmp/package/bxn-install-base
INSTALLZIP="bxn-install-base-$PACKAGEVERSION-x64-$CURRENTTIME-$USER.zip"
LINUXINSTALLZIP="bxn-install-base-linux-$PACKAGEVERSION-x64-$CURRENTTIME-$USER.zip"
echo tmp package base dir:$BASE
rm -rf $BASE
export LC_CTYPE=en_US.UTF-8
echo svn -q export svn://svnv6/bxn-structure/$svnFolder/$STRUCTUREVERSION $BASE 
svn -q export svn://svnv6/bxn-structure/$svnFolder/$STRUCTUREVERSION $BASE --username tiger --password svn555666

VERSIONALL=$BASE/bx_app/config/versionall.properties
echo write versions to $VERSIONALL
if [ ! -f "$VERSIONALL" ];then
	touch $VERSIONALL;
fi
echo version file is $VERSIONALL
echo "bxn-structure="$STRUCTUREVERSION >> $VERSIONALL;
echo "bxn-integration-uic="$VERSIONUIC  >> $VERSIONALL;
echo "bxn-core-platform="$VERSIONPLATFORM  >> $VERSIONALL;
echo "cas-server="$VERSIONCAS  >> $VERSIONALL;
echo "bxn-score="$VERSIONSCORE  >> $VERSIONALL;
echo "bxn-timetable="$VERSIONTIMETABLE  >> $VERSIONALL;
echo "bxn-microsite="$VERSIONMICROSITE  >> $VERSIONALL;
echo "bxn-portal="$VERSIONPORTAL  >> $VERSIONALL;
echo "bxn-integration-office="$VERSIONOFFICE  >> $VERSIONALL;
echo "bxn-static-resource="$VERSIONSTATIC  >> $VERSIONALL;

sh /usr/packagetools/download-bxn.sh   svnv6 bxn-integration-uic    $svnFolder $VERSIONUIC
sh /usr/packagetools/download-bxn.sh   svnv6 bxn-core-platform      $svnFolder $VERSIONPLATFORM
sh /usr/packagetools/download-bxn.sh   svnv6 cas-server             $svnFolder $VERSIONCAS/cas-server-webapp
sh /usr/packagetools/download-bxn.sh   svnv6 bxn-score              $svnFolder $VERSIONSCORE
sh /usr/packagetools/download-bxn.sh   svnv6 bxn-timetable          $svnFolder $VERSIONTIMETABLE
sh /usr/packagetools/download-bxn.sh   svnv6 bxn-microsite          $svnFolder $VERSIONMICROSITE
sh /usr/packagetools/download-bxn.sh   svnv6 bxn-portal             $svnFolder $VERSIONPORTAL
sh /usr/packagetools/download-bxn.sh   svnv6 bxn-integration-office $svnFolder $VERSIONOFFICE
sh /usr/packagetools/download-bxn.sh   svnv6 bxn-static-resource    $svnFolder $VERSIONSTATIC

echo "$(date +%Y%m%d%H%M%S) zip linux begin"
#打包linux安装版
LINUXBASE=/tmp/package/bxn-install-base-linux
rm -rf $LINUXBASE
mkdir -p $LINUXBASE
cp -r /var/nas/bxn-install $LINUXBASE/
cp -r $BASE/bx_app/config $LINUXBASE/bxn-install/
cp -r $BASE/bx_app/f_data $LINUXBASE/bxn-install/
cp -r $BASE/bx_app/data/init $LINUXBASE/bxn-install/data/
sed -i "s/^school.name =.*/school.name={school.name}\n/g" $LINUXBASE/bxn-install/config/common.properties
sed -i "s/^user_org.preload.active=.*/user_org.preload.active=true\n/g" $LINUXBASE/bxn-install/config/uic.properties
sed -i "s/^bxn.allow_inactive_user_login=.*/bxn.allow_inactive_user_login=true\n/g" $LINUXBASE/bxn-install/config/uic.properties
test ! -d "$LINUXBASE/bxn-install/apps" && mkdir -p $LINUXBASE/bxn-install/apps
for tomcat in "tomcat7pf" "tomcat7apps1" "tomcat7apps2"; do 
	cd $BASE/bx_app/program/$tomcat/webapps/
	echo `pwd`": "`ls`
	for war in `ls`; do 
		zip -qr $LINUXBASE/bxn-install/apps/$war.zip $war
	done
done
cd $BASE/bx_app/program/static
zip -qr $LINUXBASE/bxn-install/apps/bxn-static-resource.zip bxn-static-resource
cd $LINUXBASE
zip -r $LINUXINSTALLZIP *
mv $LINUXINSTALLZIP /opt/build/$TODAY/
rm -rf $LINUXBASE
echo "package linux end"

cd $BASE
echo " $(date +%Y%m%d%H%M%S)zip windows begin"
zip -r $INSTALLZIP *
mv $INSTALLZIP /opt/build/$TODAY/
echo "package windows end"
rm -rf $BASE
endMS=`date +%s`
costMs=$[$endMS-$startMS]
echo "$(date +%Y%m%d%k%M%S) package end, cost $costMs seconds.".
